package com.example.sample.activity;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.example.app.BaseActivity;
import com.example.sample.R;

import net.liteheaven.xblib.common.log.XLog;

/**
 * 说明：执行的是新Fragment的方法，但是新fragment会add失败，因为FragmentManager里边已经有了。通过日志查看整个流程
 * Created by liangy on 2016/10/14.
 */
public class FragmentProblemActivity extends BaseActivity {

    private static final String TAG = "FragmentProblemActivityTAG";
    private ProblemFragment problemFragment;
    private Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        XLog.i(TAG, "onCreate");
        setContentView(R.layout.activity_fragment_problem);
        Button button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(FragmentProblemActivity.this, FragmentProblem2Activity.class);
                intent.putExtra("arg", "123456");
                startActivity(intent);
            }
        });

        problemFragment = new ProblemFragment();
        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.fragment_container, problemFragment);
        fragmentTransaction.commit();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        XLog.i(TAG, "onNewIntent");
        problemFragment.doSomething();
//        mHandler.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                problemFragment.doSomething();
//            }
//        }, 0);
    }

    public static class ProblemFragment extends Fragment{
        private Context mContext;
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            XLog.i(TAG, "onCreateView " + this.toString());
            View v = super.onCreateView(inflater, container, savedInstanceState);
            return v;
        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            XLog.i(TAG, "onActivityCreated " + this.toString());
            super.onActivityCreated(savedInstanceState);
            mContext = getActivity();
        }

        public void doSomething(){
            XLog.i(TAG, "doSomething " + this.toString());
            mContext.getResources();
        }
    }
}
